package com.wgh.product.controller;

import com.wgh.common.vo.PageResult;
import com.wgh.common.vo.Result;
import com.wgh.product.converter.BrandConverter;
import com.wgh.product.dto.BrandDTO;
import com.wgh.product.entity.shop.Brand;
import com.wgh.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 品牌Controller
 * 提供品牌相关的RESTful API接口
 */
@RestController
@RequestMapping("/api/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    
    @Autowired
    private BrandConverter brandConverter;

    /**
     * 根据ID查询品牌信息
     */
    @GetMapping("/{id}")
    public Result<BrandDTO> getBrandById(@PathVariable Long id) {
        try {
            Brand brand = brandService.getById(id);
            if (brand == null) {
                return Result.error("品牌不存在");
            }
            BrandDTO brandDTO = brandConverter.toDTO(brand);
            return Result.success(brandDTO);
        } catch (Exception e) {
            return Result.error("查询品牌失败：" + e.getMessage());
        }
    }

    /**
     * 批量查询品牌信息
     */
    @GetMapping("/list")
    public Result<List<BrandDTO>> getBrandListByIds(@RequestParam("ids") List<Long> ids) {
        try {
            List<Brand> brands = brandService.listByIds(ids);
            List<BrandDTO> brandDTOs = brandConverter.toDTOList(brands);
            return Result.success(brandDTOs);
        } catch (Exception e) {
            return Result.error("批量查询品牌失败：" + e.getMessage());
        }
    }

    /**
     * 查询所有启用的品牌
     */
    @GetMapping("/enabled")
    public Result<List<BrandDTO>> getEnabledBrands() {
        try {
            List<Brand> brands = brandService.getEnabledBrands();
            List<BrandDTO> brandDTOs = brandConverter.toDTOList(brands);
            return Result.success(brandDTOs);
        } catch (Exception e) {
            return Result.error("查询启用品牌失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询品牌列表
     */
    @GetMapping("/page")
    public Result<List<BrandDTO>> getBrandPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            List<Brand> brands = brandService.getBrandPage(page, size);
            List<BrandDTO> brandDTOs = brandConverter.toDTOList(brands);
            return Result.success(brandDTOs);
        } catch (Exception e) {
            return Result.error("分页查询品牌失败：" + e.getMessage());
        }
    }

    /**
     * 搜索品牌
     */
    @GetMapping("/search")
    public Result<List<BrandDTO>> searchBrands(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            List<Brand> brands = brandService.searchBrands(keyword);
            // 手动分页
            int start = (page - 1) * size;
            int end = Math.min(start + size, brands.size());
            List<Brand> pagedBrands = brands.subList(start, end);
            
            List<BrandDTO> brandDTOs = brandConverter.toDTOList(pagedBrands);
            return Result.success(brandDTOs);
        } catch (Exception e) {
            return Result.error("搜索品牌失败：" + e.getMessage());
        }
    }

    /**
     * 新增品牌
     */
    @PostMapping
    public Result<Boolean> addBrand(@RequestBody BrandDTO brandDTO) {
        try {
            Brand brand = brandConverter.toEntity(brandDTO);
            boolean result = brandService.save(brand);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("新增品牌失败");
            }
        } catch (Exception e) {
            return Result.error("新增品牌失败：" + e.getMessage());
        }
    }

    /**
     * 更新品牌
     */
    @PutMapping
    public Result<Boolean> updateBrand(@RequestBody BrandDTO brandDTO) {
        try {
            Brand brand = brandConverter.toEntity(brandDTO);
            boolean result = brandService.updateById(brand);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("更新品牌失败");
            }
        } catch (Exception e) {
            return Result.error("更新品牌失败：" + e.getMessage());
        }
    }

    /**
     * 删除品牌
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteBrand(@PathVariable Long id) {
        try {
            boolean result = brandService.removeById(id);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("删除品牌失败");
            }
        } catch (Exception e) {
            return Result.error("删除品牌失败：" + e.getMessage());
        }
    }

    /**
     * 批量更新品牌状态
     */
    @PutMapping("/status")
    public Result<Boolean> updateBrandStatusBatch(@RequestParam List<Long> ids, @RequestParam Integer status) {
        try {
            boolean result = brandService.updateStatusBatch(ids, status);
            if (result) {
                return Result.success(true);
            } else {
                return Result.error("批量更新品牌状态失败");
            }
        } catch (Exception e) {
            return Result.error("批量更新品牌状态失败：" + e.getMessage());
        }
    }

    /**
     * 查询品牌关联的商品数量
     */
    @GetMapping("/{brandId}/product-count")
    public Result<Integer> countProductsByBrandId(@PathVariable Long brandId) {
        try {
            int count = brandService.countProductsByBrandId(brandId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("查询品牌关联商品数量失败：" + e.getMessage());
        }
    }

    /**
     * 根据品牌首字母查询品牌
     */
    @GetMapping("/first-letter/{firstLetter}")
    public Result<List<BrandDTO>> getBrandsByFirstLetter(@PathVariable String firstLetter) {
        try {
            List<Brand> brands = brandService.getBrandsByFirstLetter(firstLetter);
            List<BrandDTO> brandDTOs = brandConverter.toDTOList(brands);
            return Result.success(brandDTOs);
        } catch (Exception e) {
            return Result.error("根据首字母查询品牌失败：" + e.getMessage());
        }
    }
}